package com.cw.jeeyt.service.impl.git;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.service.bo.git.GitBranchSqlManageBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.git.GitBranchSqlManageDao;
import com.cw.jeeyt.dao.model.git.GitBranchSqlManageDo;
import com.cw.jeeyt.service.intf.git.GitBranchSqlManageService;
import com.cw.jeeyt.service.converter.git.GitBranchSqlManageConverter;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * git数据库脚本管理业务实现类
 *
 * @author chenw
 * @date 2019-02-25 01:04:34
 */
@Service
public class GitBranchSqlManageServiceImpl implements GitBranchSqlManageService {

    private static final Logger logger = LoggerFactory.getLogger(GitBranchSqlManageServiceImpl.class);
    @Autowired
    private GitBranchSqlManageDao gitBranchSqlManageDao;//git数据库脚本管理数据库操作接口

    /**
     * 查询所有git数据库脚本管理
     */
    @Override
    public List<GitBranchSqlManageBo> queryGitBranchSqlManageList() throws Exception {
        return GitBranchSqlManageConverter.doList2BoList(gitBranchSqlManageDao.queryGitBranchSqlManageList());
    }

    /**
     * 分页查询git数据库脚本管理
     *
     * @param gitBranchSqlManageBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<GitBranchSqlManageBo> queryByPage(GitBranchSqlManageBo gitBranchSqlManageBo, Integer startNum,
        Integer pageSize, Order order) throws Exception {

        if (gitBranchSqlManageBo == null) {
            logger.error("git数据库脚本管理为空");
            return null;
        }
        PageInfo<GitBranchSqlManageDo> pageInfo = gitBranchSqlManageDao.queryByPage(
            GitBranchSqlManageConverter.bo2Do(gitBranchSqlManageBo), startNum, pageSize, order);
        return new PageInfo<>(pageInfo, GitBranchSqlManageConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查询git数据库脚本管理信息
     *
     * @return
     */
    @Override
    public GitBranchSqlManageBo getGitBranchSqlManageById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }
        return GitBranchSqlManageConverter.do2Bo(gitBranchSqlManageDao.getById(id));
    }

    /**
     * 新增git数据库脚本管理
     *
     * @param gitBranchSqlManageBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(GitBranchSqlManageBo gitBranchSqlManageBo) throws Exception {

        if (gitBranchSqlManageBo == null) {
            logger.error("git数据库脚本管理为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitBranchSqlManageDo gitBranchSqlManage = GitBranchSqlManageConverter.bo2Do(gitBranchSqlManageBo);
        gitBranchSqlManage.setCreateBy(userBo.getId());
        gitBranchSqlManage.setCreateDate(new Date());
        gitBranchSqlManage.setUpdateBy(userBo.getId());
        gitBranchSqlManage.setUpdateDate(new Date());
        Integer result = gitBranchSqlManageDao.insert(gitBranchSqlManage);
        return result;
    }

    /**
     * 新增git数据库脚本管理不为空的字段
     *
     * @param gitBranchSqlManageBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(GitBranchSqlManageBo gitBranchSqlManageBo) throws Exception {

        if (gitBranchSqlManageBo == null) {
            logger.error("git数据库脚本管理为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitBranchSqlManageDo gitBranchSqlManage = GitBranchSqlManageConverter.bo2Do(gitBranchSqlManageBo);
        gitBranchSqlManage.setCreateBy(userBo.getId());
        gitBranchSqlManage.setCreateDate(new Date());
        gitBranchSqlManage.setUpdateBy(userBo.getId());
        gitBranchSqlManage.setUpdateDate(new Date());
        Integer result = gitBranchSqlManageDao.insertSelective(gitBranchSqlManage);
        return result;
    }

    /**
     * 更新git数据库脚本管理
     *
     * @param gitBranchSqlManageBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(GitBranchSqlManageBo gitBranchSqlManageBo) throws Exception {

        if (gitBranchSqlManageBo == null) {
            logger.error("git数据库脚本管理为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitBranchSqlManageDo gitBranchSqlManage = GitBranchSqlManageConverter.bo2Do(gitBranchSqlManageBo);
        gitBranchSqlManage.setUpdateBy(userBo.getId());
        gitBranchSqlManage.setUpdateDate(new Date());

        gitBranchSqlManageDao.update(gitBranchSqlManage);
    }

    /**
     * 更新git数据库脚本管理不为空的字段
     *
     * @param gitBranchSqlManageBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(GitBranchSqlManageBo gitBranchSqlManageBo) throws Exception {

        if (gitBranchSqlManageBo == null) {
            logger.error("git数据库脚本管理为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        GitBranchSqlManageDo gitBranchSqlManage = GitBranchSqlManageConverter.bo2Do(gitBranchSqlManageBo);
        gitBranchSqlManage.setUpdateBy(userBo.getId());
        gitBranchSqlManage.setUpdateDate(new Date());

        gitBranchSqlManageDao.updateSelective(gitBranchSqlManage);
    }

    /**
     * 删除git数据库脚本管理
     *
     * @param gitBranchSqlManageBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(GitBranchSqlManageBo gitBranchSqlManageBo) throws Exception {

        if (gitBranchSqlManageBo == null) {
            logger.error("git数据库脚本管理为空");
            return;
        }
        GitBranchSqlManageDo gitBranchSqlManage = GitBranchSqlManageConverter.bo2Do(gitBranchSqlManageBo);
        gitBranchSqlManageDao.delete(gitBranchSqlManage);
    }
}
